package cn.iocoder.yudao.module.pricehistory.service.pricehistory1;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pricehistory.controller.admin.pricehistory1.vo.*;
import cn.iocoder.yudao.module.pricehistory.dal.dataobject.pricehistory1.PriceHistoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pricehistory.dal.mysql.pricehistory1.PriceHistoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pricehistory.enums.ErrorCodeConstants.*;

/**
 * 记录旅游线路价格的变动历史 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PriceHistoryServiceImpl implements PriceHistoryService {

    @Resource
    private PriceHistoryMapper priceHistoryMapper;

    @Override
    public Long createPriceHistory(PriceHistorySaveReqVO createReqVO) {
        // 插入
        PriceHistoryDO priceHistory = BeanUtils.toBean(createReqVO, PriceHistoryDO.class);
        priceHistoryMapper.insert(priceHistory);
        // 返回
        return priceHistory.getHistoryId();
    }

    @Override
    public void updatePriceHistory(PriceHistorySaveReqVO updateReqVO) {
        // 校验存在
        validatePriceHistoryExists(updateReqVO.getHistoryId());
        // 更新
        PriceHistoryDO updateObj = BeanUtils.toBean(updateReqVO, PriceHistoryDO.class);
        priceHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deletePriceHistory(Long id) {
        // 校验存在
        validatePriceHistoryExists(id);
        // 删除
        priceHistoryMapper.deleteById(id);
    }

    private void validatePriceHistoryExists(Long id) {
        if (priceHistoryMapper.selectById(id) == null) {
            throw exception(PRICE_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public PriceHistoryDO getPriceHistory(Long id) {
        return priceHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<PriceHistoryDO> getPriceHistoryPage(PriceHistoryPageReqVO pageReqVO) {
        return priceHistoryMapper.selectPage(pageReqVO);
    }

}